package com.ljc.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.ljc.dto.Result;
import com.ljc.entity.DevicePurchasePlanning;
import com.ljc.entity.DevicePurchasePlanningArgument;
import com.ljc.service.DevicePurchasePlanningArgumentService;
import com.ljc.util.JWTUtil;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.HttpConstraint;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@CrossOrigin
@ResponseBody
@Slf4j
@RequestMapping("argument")
public class DevicePurchasePlanningArgumentController {
    @Autowired
    private DevicePurchasePlanningArgumentService devicePurchasePlanningArgumentService;

    @RequestMapping("findByExpertAndProject")
    public Result findByExpertAndProject(HttpServletRequest request, int projectPlanningId){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        List<DevicePurchasePlanning>
                deviceArgument = devicePurchasePlanningArgumentService.findByExpertAndProject(projectPlanningId,Integer.valueOf(id));
        result.setStatus(1).setMsg("成功").setData(deviceArgument);
        return result;
    }
    @RequestMapping("argumentDevice")
    public Result argumentDevice(HttpServletRequest request, @RequestBody DevicePurchasePlanningArgument argument){
        Result result = new Result();
        argument.setDate(new Date());
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        try {
            devicePurchasePlanningArgumentService.argument(argument,Integer.valueOf(id));
            result.setStatus(1).setMsg("成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(0).setMsg("失败");
        }
        return result;
    }
    @RequestMapping("getById")
    public Result getById(@RequestParam int id){
        Result result = new Result();
        DevicePurchasePlanningArgument byId = devicePurchasePlanningArgumentService.getById(id);
        result.setStatus(1).setMsg("成功").setData(byId);
        return result;
    }
    @RequestMapping("getByDevicePurchasePlanningId")
    public Result getByDevicePurchasePlanningId(@RequestParam int id){
        Result result = new Result();
        List<DevicePurchasePlanningArgument> deviceArguments = devicePurchasePlanningArgumentService.getByDevicePurchasePlanningId(id);
        result.setStatus(1).setMsg("获取设论证信息成功").setData(deviceArguments);
        return result;
    }


}
